using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CrystalSkill : Skill
{
    [SerializeField] private float existDuration = 1.5f;
    [SerializeField] private GameObject crystalPrefab;
    private GameObject currentCrystal;

    [Header("explosion info")]
    [SerializeField] private bool canExplode;

    [Header("moving info")]
    [SerializeField] private bool canMoveToEnemy;
    [SerializeField] private float moveSpeed;

    [Header("upgrade info")]
    [SerializeField] private bool canUseStackOfCrystal;
    [SerializeField] private int amountOfStacks;
    [SerializeField] private float stackCoolDown = 2;
    [SerializeField] private List<GameObject> crystalLeft = new List<GameObject>();


    public void CreateCrystal()
    {
        currentCrystal = Instantiate(crystalPrefab, player.transform.position + Vector3.up, Quaternion.identity);
        CrystalController cc = currentCrystal.GetComponent<CrystalController>();
        cc.SetupCrystal(existDuration, canExplode, canMoveToEnemy, moveSpeed, FindClosestEnemy(cc.transform));

    }

    private void ChangePlayerPosToCrystal()
    {
        if (canMoveToEnemy) return;
        Vector3 pos = player.transform.position + Vector3.up;
        player.transform.position = currentCrystal.transform.position + Vector3.down;
        currentCrystal.transform.position = pos;
        currentCrystal.GetComponent<CrystalController>().FinishCrystal();
    }


    public override void UseSkill()
    {
        base.UseSkill();
        if (CanUseCrystal())
            return;
        if (currentCrystal == null)
        {
            CreateCrystal();
        }
        else
        {
            ChangePlayerPosToCrystal();
        }
    }

    private bool CanUseCrystal()
    {
        if (canUseStackOfCrystal)
        {
            if (crystalLeft.Count == 0)
                ResetAbility();
            if (crystalLeft.Count > 0)
            {
                cooldown = 0;
                GameObject pref = crystalLeft[crystalLeft.Count - 1];
                GameObject go = Instantiate(pref, player.transform.position + Vector3.up, Quaternion.identity);
                CrystalController cc = go.GetComponent<CrystalController>();
                cc.SetupCrystal(existDuration, canExplode, canMoveToEnemy, moveSpeed, FindClosestEnemy(cc.transform));
                crystalLeft.Remove(pref);
                if (crystalLeft.Count <= 0)
                {
                    cooldown = stackCoolDown;
                    InitCrystalLeft();
                }
                return true;
            }

        }
        return false;

    }

    void InitCrystalLeft()
    {
        int spawnNum = amountOfStacks - crystalLeft.Count;
        for (int i = 0; i < spawnNum; i++)
        {
            crystalLeft.Add(crystalPrefab);
        }
    }

    void ResetAbility()
    {
        //if (cooldownTimer > 0)
        //    return;
        cooldownTimer = stackCoolDown;
        InitCrystalLeft();
    }
}
